package com.mfh.auto.create.mvvm.autocreatemvvm.file.v1

import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
import com.mfh.auto.create.mvvm.autocreatemvvm.Config
import com.mfh.auto.create.mvvm.autocreatemvvm.enums.EType
import com.mfh.auto.create.mvvm.autocreatemvvm.camelCaseToUnderscore
import com.mfh.auto.create.mvvm.autocreatemvvm.executeCouldRollBackAction
import com.mfh.auto.create.mvvm.autocreatemvvm.file.BaseFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.filetype.KotlinFileType
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CreateFileUtils

object LayoutFileModel : BaseFileModel() {

    override fun buildFileName(): String {
        val underscore = Config.name.camelCaseToUnderscore()
        return when (Config.type) {
            EType.ACTIVITY -> "activity_$underscore.xml"
            EType.FRAGMENT -> "fragment_$underscore.xml"
            EType.HOLDER -> "item_$underscore.xml"
            EType.DIALOG -> "dialog_$underscore.xml"
            else -> "dialog_$underscore.xml"
        }
    }

    override fun getFileType() = XmlFileType.INSTANCE

    override fun buildContentString(directory: PsiDirectory): String {
        return buildString {
            appendLine(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                        "<layout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                        "    xmlns:tools=\"http://schemas.android.com/tools\">\n" +
                        "\n" +
                        "    <data>\n"
            )
            when (Config.type) {
                EType.HOLDER -> {
                    appendLine(
                        "        <variable\n" +
                                "            name=\"bean\"\n" +
                                "            type=\"${Config.libPackageName}.bean.FileBean\" />"
                    )
                }

                EType.DIALOG -> {
                    appendLine(
                        "        <variable\n" +
                                "            name=\"model\"\n" +
                                "            type=\"${CreateFileUtils.getPackageByDirectory(directory)}.${Config.name}DialogModel\" />"
                    )
                }

                else -> {
                    appendLine(
                        "        <variable\n" +
                                "            name=\"viewModel\"\n" +
                                "            type=\"${CreateFileUtils.getPackageByDirectory(directory)}.${Config.name}ViewModel\" />"
                    )
                }
            }
            appendLine(
                "    </data>\n" +
                        "\n" +
                        "    <LinearLayout\n" +
                        "        android:layout_width=\"match_parent\"\n" +
                        "        android:layout_height=\"match_parent\"\n" +
                        "        android:orientation=\"vertical\">\n" +
                        "\n" +
                        "\n" +
                        "    </LinearLayout>\n" +
                        "\n" +
                        "</layout>"
            )
        }
    }

    override fun createFile(directory: PsiDirectory) {
        val psiFileFactory = PsiFileFactory.getInstance(directory.project)
        executeCouldRollBackAction(directory.project) {
            val file = psiFileFactory.createFileFromText(
                buildFileName(),
                KotlinFileType(),
                buildContentString(directory)
            )
            try {
                findLayoutDirectory(directory)?.add(file)
            } catch (e: Exception) {
            }
        }
    }

    /**
     * 获取布局文件夹
     */
    private fun findLayoutDirectory(directory: PsiDirectory): PsiDirectory? {
        var dir: PsiDirectory? = directory
        var result: PsiDirectory? = null
        while (dir?.parent != null && result == null) {
            dir = dir.parent
            if (dir!!.findSubdirectory("res") != null
                && dir.findSubdirectory("res")!!.findSubdirectory("layout") != null
            ) {
                result = dir.findSubdirectory("res")!!.findSubdirectory("layout")
            }
        }
        return result
    }

}